home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 23 web forms and controls / firstwebforms / validationform.aspx.vb < prev    next >
Encoding:
Text File  |  2002-03-17  |  5.3 KB  |  110 lines

  1. Public Class ValidationForm
  2.     Inherits System.Web.UI.Page
  3.     Protected WithEvents Label1 As System.Web.UI.WebControls.Label
  4.     Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
  5.     Protected WithEvents Label2 As System.Web.UI.WebControls.Label
  6.     Protected WithEvents RangeValidator1 As System.Web.UI.WebControls.RangeValidator
  7.     Protected WithEvents Label3 As System.Web.UI.WebControls.Label
  8.     Protected WithEvents CompareValidator1 As System.Web.UI.WebControls.CompareValidator
  9.     Protected WithEvents Label4 As System.Web.UI.WebControls.Label
  10.     Protected WithEvents RegularExpressionValidator1 As System.Web.UI.WebControls.RegularExpressionValidator
  11.     Protected WithEvents Label5 As System.Web.UI.WebControls.Label
  12.     Protected WithEvents CustomValidator1 As System.Web.UI.WebControls.CustomValidator
  13.     Protected WithEvents ValidationSummary1 As System.Web.UI.WebControls.ValidationSummary
  14.     Protected WithEvents Button1 As System.Web.UI.WebControls.Button
  15.     Protected WithEvents txtUserName As System.Web.UI.WebControls.TextBox
  16.     Protected WithEvents txtYearBorn As System.Web.UI.WebControls.TextBox
  17.     Protected WithEvents txtYearMarried As System.Web.UI.WebControls.TextBox
  18.     Protected WithEvents txtPhoneNumber As System.Web.UI.WebControls.TextBox
  19.     Protected WithEvents Label7 As System.Web.UI.WebControls.Label
  20.     Protected WithEvents CompareValidator2 As System.Web.UI.WebControls.CompareValidator
  21.     Protected WithEvents txtLastVisit As System.Web.UI.WebControls.TextBox
  22.     Protected WithEvents Label8 As System.Web.UI.WebControls.Label
  23.     Protected WithEvents txtChildren As System.Web.UI.WebControls.TextBox
  24.     Protected WithEvents CompareValidator3 As System.Web.UI.WebControls.CompareValidator
  25.     Protected WithEvents txtEvenNumber As System.Web.UI.WebControls.TextBox
  26.     Protected WithEvents lblStatus As System.Web.UI.WebControls.Label
  27.     Protected WithEvents Literal1 As System.Web.UI.WebControls.Literal
  28.  
  29. #Region " Web Form Designer Generated Code "
  30.  
  31.     'This call is required by the Web Form Designer.
  32.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  33.  
  34.     End Sub
  35.  
  36.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
  37.         'CODEGEN: This method call is required by the Web Form Designer
  38.         'Do not modify it using the code editor.
  39.         InitializeComponent()
  40.     End Sub
  41.  
  42. #End Region
  43.  
  44.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  45.         'Put user code to initialize the page here
  46.  
  47.         ' this constant means that the JavaScript of the custom
  48.         ' function is stored right in the HTML page
  49.         ' (If you set this constant to False you must delete the
  50.         ' <script> portion of the HTML page)
  51. #Const EMBED_CUSTOM_FUNCTION_IN_HTML = True
  52.  
  53. #If EMBED_CUSTOM_FUNCTION_IN_HTML = False Then
  54.         ' this block of code shows how you can embed the JavaScript
  55.         ' routine via code, by assigning it to a Literal control
  56.         Literal1.Text = "<script language=""JScript""><!-- " & ControlChars.CrLf _
  57.             & "function CheckEvenNumber(sender, args) {" & ControlChars.CrLf _
  58.             & "  var theNumber = args.Value;" & ControlChars.CrLf _
  59.             & "  if (theNumber % 2 == 0) " & ControlChars.CrLf _
  60.             & "    args.IsValid = true;" & ControlChars.CrLf _
  61.             & "  else " & ControlChars.CrLf _
  62.             & "    args.IsValid = false;" & ControlChars.CrLf _
  63.             & "} " & ControlChars.CrLf _
  64.             & "//--> " & ControlChars.CrLf _
  65.             & "</script>" & ControlChars.CrLf
  66. #End If
  67.  
  68.         ' this code shows how you can remember the intial non-null
  69.         ' value of the txtUserName control.
  70.         If txtUserName.Attributes("InitialValue") = "" Then
  71.             If txtUserName.Text.Length > 0 Then
  72.                 txtUserName.Attributes.Add("initialvalue", txtUserName.Text)
  73.             End If
  74.         End If
  75.     End Sub
  76.  
  77.     ' this is the server-side version of the custom validate function
  78.  
  79.     Private Sub CustomValidator1_ServerValidate(ByVal source As System.Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
  80.         Dim theNumber As Integer = CInt(args.Value)
  81.         If (theNumber Mod 2) = 0 Then
  82.             args.IsValid = True
  83.         Else
  84.             args.IsValid = False
  85.         End If
  86.     End Sub
  87.  
  88.     ' this routine runs when the form is submitted
  89.  
  90.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  91.         ' Force the validation of all controls.
  92.         Me.Validate()
  93.  
  94.         ' the form might be nonvalid only if working on a browser
  95.         ' that doesn't support JavaScript, or if we've set the
  96.         ' EnableClientScript property to False
  97.  
  98.         If Not Me.IsValid Then
  99.             Dim ctrl As BaseValidator
  100.             Dim errorCount As Integer
  101.             ' Count how many errors were found.
  102.             For Each ctrl In Me.Validators
  103.                 If Not ctrl.IsValid Then errorCount += 1
  104.             Next
  105.             ' Display a suitable error message.
  106.             lblStatus.Text = "There are " & errorCount.ToString & " errors."
  107.         End If
  108.     End Sub
  109. End Class
  110.